﻿using System;
using System.Linq;
using System.Web.Helpers;

namespace User.EF.Model
{
    /// <summary>
    /// Anlegen von Standardeinträgen für die DB.
    /// </summary>
    public class CreateBaseData
    {
        /// <summary>
        /// Anlegen unseres Admin Users.
        /// </summary>
        /// <param name="container"></param>
        public static void CreateAdminUser(MainModelContainer container)
        {
            //Nur anlegen, wenn noch kein User existiert.
            if (container.MemberSet.Count() == 0)
            {
                Member member = new Member();
                member.Username = "squadwuschel@gmx.de";
                member.CreationDate = DateTime.Now;
                member.LastLoginTry = DateTime.Now;
                member.LoginTries = 0;
                member.IsActive = true;
                member.PasswordSalt = Crypto.GenerateSalt();
                member.Password = Crypto.HashPassword(string.Format("{0}{1}", "squadwuschel", member.PasswordSalt));
                member.PasswordCheangedDate = DateTime.Now;
                member.MemberExtension = new MemberExtension();
                member.MemberExtension.Title = ETitle.Herr;
                member.MemberExtension.Firstname = "Squad";
                member.MemberExtension.Lastname = "Wuschel";
                member.MemberExtension.Mobilenumber = "0155/6234567";
                member.PasswordVerifivationTokenExpirationDate = DateTime.Now;
                container.MemberSet.Add(member);
                container.SaveChanges();

                container.MemberRightsSet.Add(new MemberRights() { MemberId = member.Id, MemberRight = EMemberRight.Administrator });
                container.MemberRightsSet.Add(new MemberRights() { MemberId = member.Id, MemberRight = EMemberRight.MemberEdit });
                container.MemberRightsSet.Add(new MemberRights() { MemberId = member.Id, MemberRight = EMemberRight.MemberRights });
                container.SaveChanges();
            }
        }

        /// <summary>
        /// Anlegen von ein paar Standardtestbenutzern
        /// </summary>
        public static void CreateDefaultUsers(MainModelContainer container)
        {
            //Nur anlegen, wenn noch kein User existiert.
            if (container.MemberSet.Count() == 1)
            {
                string[] firstNames = {"Johannes", "Kai", "Thomas", "Jonathan", "Stefanie", "Marie", "Ron", "Jon"};
                string[] lastNames = {"Müller", "Mayer", "Brandt", "Hawkins", "Muster", "Gates", "Ivy"};
                string[] mailProvider = {"gmx", "hotmail", "aol", "gmail", "google"};

                Random rand = new Random();

                for (int i = 0; i < 20; i++)
                {
                    Member member = new Member();
                    member.CreationDate = DateTime.Now;
                    member.LastLoginTry = DateTime.Now;
                    member.LoginTries = 0;
                    member.IsActive = true;
                    member.PasswordSalt = Crypto.GenerateSalt();
                    member.Password = Crypto.HashPassword(string.Format("{0}{1}", "test", member.PasswordSalt));
                    member.PasswordCheangedDate = DateTime.Now;
                    member.MemberExtension = new MemberExtension();
                    member.MemberExtension.Title = rand.Next(0,i) % 2 == 1 ? ETitle.Herr : ETitle.Frau;
                    member.MemberExtension.Firstname =  firstNames[rand.Next(0,firstNames.Length)];
                    member.MemberExtension.Lastname = lastNames[rand.Next(0,lastNames.Length)];
                    member.MemberExtension.Mobilenumber = "0155/6234567" + i;
                    member.Username = string.Format("{0}.{1}@{2}.de", member.MemberExtension.Firstname, member.MemberExtension.Lastname , mailProvider[rand.Next(0, mailProvider.Length)]);
                    member.PasswordVerifivationTokenExpirationDate = DateTime.Now;
                    container.MemberSet.Add(member);
                    container.SaveChanges();
                }
            }
        }
    }
}
